//
//  ActivityIndicatorBgView.m
//  微他
//
//  Created by 微他 on 14-9-15.
//  Copyright (c) 2014年 微他. All rights reserved.
//

#import "ActivityIndicatorBgView.h"

#define viewTag 123456789

@interface ActivityIndicatorBgView ()
{
    UIActivityIndicatorView *_indicatorView;
    UIView *_view;
    UIImageView *_imageView;
}
@end

@implementation ActivityIndicatorBgView

__strong static ActivityIndicatorBgView *bgView = nil;
// 这里使用的是ARC下的单例模式
+ (ActivityIndicatorBgView *)instance
{
    // dispatch_once不仅意味着代码仅会被运行一次，而且还是线程安全的
    static dispatch_once_t pred = 0;
    dispatch_once(&pred, ^{
        bgView = [[super allocWithZone:NULL] init];
    });
    return bgView;
}
// 这里
+ (id)allocWithZone:(NSZone *)zone
{
    
    /* 这段代码无法使用, 那么我们如何解决alloc方式呢？
     dispatch_once(&pred, ^{
     singleton = [super allocWithZone:zone];
     return singleton;
     });
     */
    return [self instance];
}

- (id)copyWithZone:(NSZone *)zone
{
    return self;
}

- (void)startAnimating
{
    UIView *tempView = (UIView *)[self viewWithTag:viewTag];
    if (tempView) {
        [self stopAnimating];
        tempView = nil;
    }
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.01];
    _view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 120)];
    _view.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    _view.backgroundColor = [UIColor colorWithRed:11 / 255.0 green:11 / 255.0 blue:11 / 255.0 alpha:0.6];
    [self addSubview: _view];
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor clearColor];
    
//    NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"GIF"];
//    NSData *gifData = [NSData dataWithContentsOfFile:path];
//    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 70, 70)];
//    webView.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
//    webView.backgroundColor= [UIColor clearColor];
//    [webView setOpaque:NO];
//    webView.scalesPageToFit= YES;
//    [webView loadData:gifData MIMEType:@"image/gif"textEncodingName:nil baseURL:nil];
//    [self addSubview:webView];
    
    
    //设置动画
    NSMutableArray *imgArray = [NSMutableArray array];
    for (int i=1; i<13; i++) {
        UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"背景%d",i]];
        [imgArray addObject:img];
    }
    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(270, 210, 35, 35)];
    _imageView.center = CGPointMake(60, 15);
    [bgView addSubview:_imageView];
    _imageView.animationImages = imgArray;
    _imageView.animationDuration = 0.8;
    _imageView.animationRepeatCount = -1;
    [_imageView startAnimating];
    
//    _indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
//    _indicatorView.center = CGPointMake(60, 10);
//    [bgView addSubview:_indicatorView];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, _imageView.center.y + 35, _view.bounds.size.width, 16)];
    label.text = @"请稍等";
    label.textColor = [UIColor whiteColor];
    [label setFont:[UIFont systemFontOfSize:16]];
    //设置字体居中
    label.textAlignment=NSTextAlignmentCenter;
    [bgView addSubview:label];
    bgView.frame = CGRectMake(0, 0, 120, CGRectGetMaxY(label.frame));
    bgView.center = CGPointMake(CGRectGetMidX(_view.bounds), CGRectGetMidY(_view.bounds));
    [_view addSubview:bgView];
    
    _view.layer.cornerRadius = 6.0;
    [_indicatorView startAnimating];
    _view.tag = viewTag;
}

- (void)stopAnimating
{
//    [_indicatorView stopAnimating];
    [_imageView stopAnimating];
    [_view removeFromSuperview];
    _view = nil;
    [self removeFromSuperview];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"cancelRequst" object:nil];
    if (_isTouch == NO) {
        [self stopAnimating];
    }

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
